package com.sd.view;

import java.util.Vector;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alex.books.BaseBook;
import com.alex.log.ALog;
import com.sd.activity.R;
import com.sd.app.AppInit;
import com.sd.util.AlexStringUtil;

public class BookTilingAdapter extends BaseAdapter {

	
	private Vector<BaseBook> mBooks;
	private LayoutInflater mLayoutInflater;
	private OnClickListener mListener;
	private OnLongClickListener mLongListener;
	private Context mContext;
	
	public BookTilingAdapter(Context context,Vector<BaseBook> books,OnClickListener listener,OnLongClickListener longListener){
		mContext = context;
		mBooks = books;
		mLayoutInflater = LayoutInflater.from(context);
		mListener = listener;
		mLongListener = longListener;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mBooks.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mBooks.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.bookview, null);
			holder.book1 = (ImageView)convertView.findViewById(R.id.text);
			holder.book2 = (TextView)convertView.findViewById(R.id.bookname);
			holder.book3 = (ImageView)convertView.findViewById(R.id.bookmark);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		if(mBooks.get(position).mBookMarksNumber>0){
			holder.book3.setVisibility(View.VISIBLE);
		}else{
			holder.book3.setVisibility(View.GONE);
		}
		convertView.setPadding(0, 11, 0, 22);
		if(position<mBooks.size() && mBooks.get(position)!=null){
		
	 		ImageView mBookMark = new ImageView(mContext);
			if(AlexStringUtil.isUnknownString(mBooks.get(position).mBookCover)){
				holder.book1.setBackgroundResource(R.drawable.testbook_bg);
			}else{
				holder.book1.setBackgroundDrawable(new BitmapDrawable(AppInit.getInstance().APP_BOOKS + mBooks.get(position).mBookCover));
			}
			if(mBooks.get(position).mAttach1 != null && mBooks.get(position).mAttach1.equals("1")){
			}else{
				holder.book2.setText(mBooks.get(position).mBookName);
			}
		}
		return convertView;
	}
	
	class ViewHolder {
		/**
		 * 做法名字
		 */
		ImageView book1;
		/**
		 * 做法介绍
		 */
		TextView book2;
		/**
		 * 做法加价
		 */
		ImageView book3;
	}
	
	
}
